package com.qianxp.designparttern.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
/**
 *@author qianxp
 */
public class CglibProxy implements MethodInterceptor {

    private CglibProxy(){
    }

    private static class InnerClass{
        static final CglibProxy cglibProxy = new CglibProxy();
    }

    public static CglibProxy getInstance(){
        return InnerClass.cglibProxy;
    }


    public  Object getProxy(Class clazz){
        Enhancer e = new Enhancer();
        e.setSuperclass(clazz);
        e.setCallback(this);
        return e.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("调用了："+method.getName() + "方法=====前");
        Object obj = methodProxy.invokeSuper(o,objects);
        System.out.println("调用了："+method.getName() + "方法=====后");
        return obj;
    }

}
